package attach

import (
	"fmt"
	"github.com/go-kratos/kratos/v2/log"
	"go-kratos-demo/internal/conf"
	"net"
	"net/rpc"
)

const ApiDemoInfo = "demo.Info"

type AttachClient struct {
	client *rpc.Client
	log    *log.Helper
	conf   *conf.Api_Client
}

func NewAttachApi(api *conf.Api, logger log.Logger) *AttachClient {
	if api.Attach.Enabled == 1 {
		c, err := net.Dial(api.Attach.Https, api.Attach.Addr)

		if err != nil {
			panic("error api" + err.Error())
		}
		client := rpc.NewClient(c)
		fmt.Println(client)
		return &AttachClient{
			conf:   api.Attach,
			client: client, log: log.NewHelper(logger),
		}
	}
	return &AttachClient{
		conf: api.Attach,
		log:  log.NewHelper(logger),
	}
}

type AttachRequest struct {
	Id   int    `json:"ID"`
	Name string `json:"filename"`
	Url  string `json:"oss_path"`
}

type AttachReply struct {
	Id   int32  `json:"ID"`
	Name string `json:"filename"`
	Url  string `json:"oss_path"`
}

func (f *AttachClient) GetAttach(req AttachRequest) (reply AttachReply, err error) {
	if f.conf.Enabled == 0 {
		return
	}
	err = f.client.Call(ApiDemoInfo, req, reply)
	return
}
